home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Environments / Small Eiffel 0.4.8 / lib_std / c_array.e < prev    next >
Encoding:
Text File  |  1997-04-13  |  6.4 KB  |  313 lines  |  [TEXT/ttxt]

  1. -- Part of SmallEiffel -- Read DISCLAIMER file -- Copyright (C) 
  2. -- Dominique COLNET and Suzanne COLLIN -- colnet@loria.fr
  3. --
  4. expanded class C_ARRAY[E]
  5.    --
  6.    -- Eiffel access to array of langage C.
  7.    -- 
  8.    -- Warning : using this class makes your Eiffel code non
  9.    -- portable on others Eiffel systems.
  10.    --
  11.  
  12. feature -- Basic features :
  13.  
  14.    calloc(capacity: INTEGER) is
  15.      -- Like the one of C ANSI using sizeof actual `E'.
  16.      -- Values are set to the default Eiffel value.
  17.       external "CSE"
  18.       end;
  19.  
  20.    item(index: INTEGER): E is
  21.      -- Assume that `malloc' is done and that `index' is the
  22.      -- range [0 .. capacity-1].
  23.       external "CSE"
  24.       end;
  25.  
  26.    put(element: E; index: INTEGER) is
  27.      -- Assume that `malloc' is done and that `index' is the
  28.      -- range [0 .. capacity-1].
  29.       external "CSE"
  30.       end;
  31.  
  32. feature 
  33.  
  34.    malloc(capacity: INTEGER) is
  35.      -- Like the one of C ANSI using sizeof actual `E'.
  36.      -- As in C, values are not initialised.
  37.       external "CSE"
  38.       end;
  39.  
  40.    realloc(old_one: like Current; capacity: INTEGER) is
  41.      -- Like the one of C ANSI using sizeof actual `E'.
  42.      -- As in C, values are not initialised.
  43.       external "CSE"
  44.       end;
  45.  
  46. feature -- Comparison :
  47.  
  48.    memcmp(other: like Current; capacity: INTEGER): BOOLEAN is
  49.      -- True if all elements in range [0..capacity-1] are
  50.      -- identical using `equal'. Assume Current and `other' 
  51.      -- are big enougth. 
  52.      -- See also `fast_memcmp'.
  53.       require
  54.      other.to_pointer.is_not_void;
  55.      capacity > 0
  56.       local
  57.      i: INTEGER;
  58.       do
  59.      from
  60.         i := capacity - 1;
  61.         Result := equal_like(item(0),other.item(0));
  62.      until
  63.         i = 0 or else not Result
  64.      loop
  65.         Result := equal_like(item(i),other.item(i));
  66.         i := i - 1;
  67.      end;
  68.       end;
  69.  
  70.    fast_memcmp(other: like Current; capacity: INTEGER): BOOLEAN is
  71.      -- Same jobs as `memcmp' but uses infix "=" instead `equal'.
  72.       require
  73.      other.to_pointer.is_not_void;
  74.      capacity > 0
  75.       local
  76.      i: INTEGER;
  77.       do
  78.      from
  79.         Result := item(0) = other.item(0);
  80.         i := capacity - 1;
  81.      until
  82.         i = 0 or else not Result
  83.      loop
  84.         Result := item(i) = other.item(i);
  85.         i := i - 1;
  86.      end;
  87.       end;
  88.  
  89. feature -- Searching :
  90.  
  91.    index_of(element: like item; upper: INTEGER): INTEGER is
  92.      -- Give the index of the first occurrence of `element' using
  93.      -- `is_equal' for comparison.
  94.      -- Answer `upper + 1' when `element' is not inside.
  95.       require
  96.      upper >= -1
  97.       do
  98.      from  
  99.      until
  100.         Result > upper or else equal_like(element,item(Result))
  101.      loop
  102.         Result := Result + 1;
  103.      end;
  104.       end;
  105.  
  106.    fast_index_of(element: like item; upper: INTEGER): INTEGER is
  107.      -- Same as `index_of' but use `=' for comparison.
  108.       require
  109.      upper >= -1
  110.       do
  111.      from  
  112.      until
  113.         Result > upper or else element = item(Result)
  114.      loop
  115.         Result := Result + 1;
  116.      end;
  117.       end;
  118.  
  119. feature -- Removing :
  120.  
  121.    remove_first(upper: INTEGER) is
  122.      -- Assume `upper' is a valid index.
  123.      -- Move range [1 .. `upper'] by 1 position left.
  124.       require
  125.      upper >= 0
  126.       local
  127.      i: INTEGER;
  128.       do
  129.      from 
  130.      until
  131.         i = upper
  132.      loop
  133.         put(item(i + 1),i);
  134.         i := i + 1;
  135.      end;
  136.       end;
  137.  
  138.    remove(index, upper: INTEGER) is
  139.      -- Assume `upper' is a valid index.
  140.      -- Move range [`index' + 1 .. `upper'] by 1 position left.
  141.       require
  142.      index >= 0;
  143.      index <= upper
  144.       local
  145.      i: INTEGER;
  146.       do
  147.      from 
  148.         i := index;
  149.      until
  150.         i = upper
  151.      loop
  152.         put(item(i + 1),i);
  153.         i := i + 1;
  154.      end;
  155.       end;
  156.  
  157. feature -- Other :
  158.  
  159.    set_all_with(v: like item; upper: INTEGER) is
  160.      -- Set all elements in range [0 .. upper] with
  161.      -- value `v'.
  162.       local
  163.      i: INTEGER;
  164.       do
  165.      from
  166.         i := upper;
  167.      until
  168.         i < 0
  169.      loop
  170.         put(v,i);
  171.         i := i - 1;
  172.      end;
  173.       end;
  174.  
  175.    copy_from(other: like Current; upper: INTEGER) is
  176.      -- Assume `upper' is a valid index both in Current
  177.      -- and `other'.
  178.       local
  179.      i: INTEGER;
  180.       do
  181.      from
  182.         i := upper;
  183.      until
  184.         i < 0
  185.      loop
  186.         put(other.item(i),i);
  187.         i := i - 1;
  188.      end;
  189.       end;
  190.  
  191.    nb_occurrences(element: like item; upper: INTEGER): INTEGER is
  192.      -- Number of occurrences of `element' in range [0..upper]
  193.      -- using `equal' for comparison.
  194.      -- See also `fast_nb_occurrences' to chose the apropriate one.
  195.       local
  196.      i: INTEGER;
  197.       do
  198.      from  
  199.         i := upper;
  200.      until
  201.         i < 0
  202.      loop
  203.         if equal_like(element,item(i)) then
  204.            Result := Result + 1;
  205.         end;
  206.         i := i - 1;
  207.      end;
  208.       end;
  209.  
  210.    fast_nb_occurrences(element: like item; upper: INTEGER): INTEGER is
  211.      -- Number of occurrences of `element' in range [0..upper]
  212.      -- using basic "=" for comparison.
  213.      -- See also `fast_nb_occurrences' to chose the apropriate one.
  214.       local
  215.      i: INTEGER;
  216.       do
  217.      from  
  218.         i := upper;
  219.      until
  220.         i < 0
  221.      loop
  222.         if element = item(i) then
  223.            Result := Result + 1;
  224.         end;
  225.         i := i - 1;
  226.      end;
  227.       end;
  228.  
  229.    hashcode(upper: INTEGER): INTEGER is
  230.       -- Assume Current type is C_ARRAY[CHARACTER].
  231.       local
  232.      i: INTEGER;
  233.       do
  234.      from
  235.         i := upper;
  236.         if i > 15 then
  237.            i := 15;
  238.         end;
  239.      until
  240.         i < 0
  241.      loop
  242.         Result := Result + item(i).code;
  243.         i := i - 1;
  244.      end;
  245.       end;
  246.  
  247.    all_cleared(upper: INTEGER): BOOLEAN is
  248.      -- Are all items in range [0..upper] set to default
  249.      -- values?
  250.       require
  251.      upper >= -1
  252.       local
  253.      i: INTEGER;
  254.      model: like item;
  255.       do
  256.      from
  257.         Result := true;
  258.         i := upper;
  259.      until
  260.         i < 0 or else not Result
  261.      loop
  262.         Result := model = item(i)
  263.         i := i - 1;
  264.      end;
  265.       end;
  266.  
  267. feature -- The Guru section :
  268.  
  269.    free is
  270.      -- Like the one of C ANSI. 
  271.      -- Assume Current is not already `free'.
  272.       external "ICWC"
  273.       end;
  274.  
  275. feature -- Interfacing with C :
  276.  
  277.    to_external: POINTER is
  278.      -- Gives access to the C pointer on the area of storage.
  279.       do
  280.      Result := to_pointer;
  281.       end;
  282.  
  283.    from_pointer(c_array_pointer: POINTER) is
  284.      -- Assign Current with `c_array_pointer'.
  285.       external "CSE"
  286.       end;
  287.  
  288.    is_not_void: BOOLEAN is
  289.       do
  290.      Result := to_pointer.is_not_void;
  291.       end;
  292.  
  293. feature {NONE}
  294.  
  295.    frozen equal_like(e1, e2: like item): BOOLEAN is
  296.      -- Note: this feature is called to avoid calling `equal'
  297.      -- on expanded types (no automatic conversion to 
  298.      -- corresponding reference type).
  299.       do
  300.      if e1.is_basic_expanded_type then
  301.         Result := e1 = e2;
  302.      elseif e1.is_expanded_type then
  303.         Result := e1.is_equal(e2);
  304.      elseif e1 = e2 then
  305.         Result := true;
  306.      elseif e1 = Void or else e2 = Void then
  307.      else
  308.         Result := e1.is_equal(e2);
  309.      end;
  310.       end;
  311.  
  312. end
  313.